package com.shbaiche.bifenba.ui.match;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.data.CompeteVo;
import com.shbaiche.bifenba.model.match.MatchesLineupMessage;
import com.shbaiche.bifenba.model.match.MatchesListMessage;
import com.shbaiche.bifenba.model.match.MatchesVo;
import com.shbaiche.bifenba.model.match.PlayerVo;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.base.BaseFragment;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;
import com.shbaiche.bifenba.view.AutoScaleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidbase.imageloader.ILFactory;
import okhttp3.Call;

/**
 * 比赛详情-阵容
 */

@SuppressLint("ValidFragment")
public class MatchDetailLineupFragment extends BaseFragment {
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout refreshLayout;

    @BindView(R.id.layout_content_intelligence)
    LinearLayout layoutIntelligence;

    @BindView(R.id.iv_empty)
    AutoScaleImageView ivEmpty;

    @BindView(R.id.layout_content)
    LinearLayout layoutContent;


    @BindView(R.id.tv_referee)
    TextView tvReferee;

    @BindView(R.id.tv_stadium)
    TextView tvStadium;


    MatchesVo mMatchVo;

    @SuppressLint("ValidFragment")
    public MatchDetailLineupFragment(){

    }

    public static MatchDetailLineupFragment newInstance() {
        return new MatchDetailLineupFragment();
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        if(savedInstanceState!=null && savedInstanceState.containsKey("data")){
            mMatchVo = (MatchesVo)savedInstanceState.getSerializable("data");
        }else{
            mMatchVo = (MatchesVo)getArguments().getSerializable("data");
        }

        loadData();


        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadData();
            }
        });
    }

    private void setViews(MatchesLineupMessage message){
        if(message!=null){
            if(TextUtils.isEmpty(message.getReferee())){
                tvReferee.setText("主裁：");
            }else{
                tvReferee.setText("主裁："+message.getReferee());
            }

            if(TextUtils.isEmpty(message.getStadium())){
                tvStadium.setText("场地：");
            }else{
                tvStadium.setText("场地："+message.getStadium());
            }



            layoutIntelligence.removeAllViewsInLayout();
            //首发
            layoutIntelligence.addView(getLineupView(1,message));

            //替补
            layoutIntelligence.addView(getLineupView(2,message));
        }else{

        }
    }

    private View getLineupView(int type,MatchesLineupMessage message){
        View view = View.inflate(getContext(),R.layout.layout_match_detail_intelligence_root,null);
        LinearLayout layoutLeft = (LinearLayout) view.findViewById(R.id.layout_content_intelligence_list_left);
        LinearLayout layoutRight = (LinearLayout) view.findViewById(R.id.layout_content_intelligence_list_right);
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
        TextView tvNameHome = (TextView) view.findViewById(R.id.tv_name_home);
        TextView tvNameVisit = (TextView) view.findViewById(R.id.tv_name_visit);

        ImageView ivLogoHome = (ImageView) view.findViewById(R.id.iv_logo_home);
        ImageView ivLogoVisit = (ImageView) view.findViewById(R.id.iv_logo_visit);

        tvNameHome.setText(message.getHomeName());
        tvNameVisit.setText(message.getVisitName());

        ILFactory.getLoader().loadNet(ivLogoHome, message.getHomeLogo(), null);
        ILFactory.getLoader().loadNet(ivLogoVisit, message.getVisitLogo(), null);

        List<PlayerVo> listLeft = null,listRight= null;
        switch (type){
            case 1:
                tvTitle.setText("首发阵容");
                listLeft = message.getHomeFirst();
                listRight = message.getVisitFirst();
                break;
            case 2:
                tvTitle.setText("替补阵容");
                listLeft = message.getHomeSub();
                listRight = message.getVisitSub();
                break;
        }

        if(listLeft!=null){
            for (int i = 0; i< listLeft.size();i++){
                PlayerVo vo = listLeft.get(i);
                layoutLeft.addView(getPlayerView(vo));
            }
        }

        if(listRight!=null){
            for (int i = 0; i< listRight.size();i++){
                PlayerVo vo = listRight.get(i);
                layoutRight.addView(getPlayerView(vo));
            }
        }

        return view;
    }

    private View getPlayerView(PlayerVo vo){
        if(vo!=null){
            View viewItem = View.inflate(getContext(),R.layout.layout_match_detail_lineup_item,null);
            TextView tvNo = (TextView) viewItem.findViewById(R.id.tv_no);
            TextView tvName = (TextView) viewItem.findViewById(R.id.tv_name);
            tvNo.setText(""+vo.getShitNo());
            tvName.setText(""+vo.getName());

            return viewItem;
        }
       return null;
    }



    public void loadData() {
        MatchesLineupMessage message = new MatchesLineupMessage();
        message.setCompeteId(mMatchVo.getCompeteId());
        message.setGameType(mMatchVo.getGameType());
        message.setGameId(mMatchVo.getGameId());

        NetApi.matchesLineup(message, new JsonCallback<TransMessage>() {
            @Override
            public void onFail(Call call, Exception e, int id) {
                refreshLayout.setRefreshing(false);
                ToastUtil.show("onFail" + e.getMessage());
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                refreshLayout.setRefreshing(false);
                ToastUtil.show("onFail" + e.getMessage());

            }

            @Override
            public void onResponse(TransMessage response, int id) {
                refreshLayout.setRefreshing(false);
                MatchesLineupMessage result =new Gson().fromJson(response.getMessage(),MatchesLineupMessage.class);
                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, result.getCode())) {
                    if(result!=null){
                        Log.e("t","onResponse");
                        if(result.getHomeFirst()!=null && result.getHomeFirst().size()>0){
                            ivEmpty.setVisibility(View.GONE);
                            layoutContent.setVisibility(View.VISIBLE);
                            setViews(result);
                        }else{
                            ivEmpty.setVisibility(View.VISIBLE);
                            layoutContent.setVisibility(View.GONE);
                        }

                    }
                }else{
                    Log.e("t","onResponse getCode "+response.getCode());
                    ToastUtil.show(result.getMessage());
                }
            }
        });
    }








    @Override
    public int getLayoutId() {
        return R.layout.fragment_match_detail_lineup;
    }
}
